package com.itheima.homework;

/**
 * @Author 王加鑫
 * @Date 2021/5/22 20:59
 * @Version 1.0
 * 3、【编号2003】某电影院目前正在上映国产大片，共有100张票，而它有3个窗口卖票，请设计一个程序模拟该电影院卖票，并保证线程同步，不会出现错误的买票数据。
 */
public class Demo03 {
    public static void main(String[] args) {


        MyRunnable runnable = new MyRunnable();

        Thread thread1 = new Thread(runnable);
        Thread thread2 = new Thread(runnable);
        Thread thread3 = new Thread(runnable);

        thread1.start();
        thread2.start();
        thread3.start();


    }
}



class MyRunnable implements Runnable{

    private int  count = 100;


    @Override
    public void run() {
        while (true){
            synchronized ("abc"){
                if(count == 0){
                    break;
                }else {
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    count--;
                    System.out.println(Thread.currentThread().getName() + "正在卖票,还剩" + count + "张票");
                }

            }
        }
    }
}
